package com.pack;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 
public class SendMailActivity extends Activity {
	
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.email);
		
		//widgets
		buttonSend = (Button) findViewById(R.id.send_button);
		buttonCancel = (Button) findViewById(R.id.cancel_button);
		textTo = (EditText) findViewById(R.id.editTextTo);
		textSubject = (EditText) findViewById(R.id.editTextSubject);
		textMessage = (EditText) findViewById(R.id.editTextMessage);
		
		//recupera i dati dell'email da inviare
		final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		String objectPref = prefs.getString("object", "");
		String textPref = prefs.getString("text", "");
		textSubject.setText(objectPref);
		textMessage.setText(textPref);
		
		Bundle bundle = this.getIntent().getExtras();
		textTo.setText(bundle.getString("contacts"));
		/*
		textSubject.setText(bundle.getString("subject"));
		textMessage.setText(bundle.getString("message"));
		*/
		
		//ClickListener associato al button "SEND"
		buttonSend.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				to = textTo.getText().toString();
				subject = textSubject.getText().toString();
				message = textMessage.getText().toString();
				
				//popola i campi dell'email
				Intent email = new Intent(Intent.ACTION_SEND);
				email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
				email.putExtra(Intent.EXTRA_SUBJECT, subject);
				email.putExtra(Intent.EXTRA_TEXT, message);
 
				//need this to prompts email client only
				email.setType("message/rfc822");
				
				//avvia il chooser per il client mail
				startActivity(Intent.createChooser(email, "Choose an Email client :"));
			}
		});
		
		//ClickListener associato al button "CANCEL"
		buttonCancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//avvia l'activity iniziale (SendMailSMSActivity)
            	Intent sendmailsms_intent = new Intent(SendMailActivity.this,MainActivity.class);
                startActivity(sendmailsms_intent);
                //e termina l'activity corrente
                SendMailActivity.this.finish();
			}
		});
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 * potrebbe essere necessario rimuovere tale metodo dopo l'integrazione con le altre parti del progetto
	 */
	/*
	@Override
	public void onResume() {
		super.onResume();
		//compare un messaggio
		Toast.makeText(getApplicationContext(), "Send a new email !",Toast.LENGTH_LONG).show();
		//resetta tutti i campi
		textTo.setText("");
		textSubject.setText("");
		textMessage.setText("");
	}
	*/
	
	//variabili globali
		private Button buttonSend, buttonCancel;
		private EditText textTo;
		private EditText textSubject;
		private EditText textMessage;
		private String to, subject, message;
		
}
